﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using log4net.Appender;
using log4net.Core;
using Tapioca.Utilities;
using System.Configuration;
using System.Xml.Linq;

namespace Tapioca.Views
{
    public partial class AppSetting : Form
    {
        public string Engine
        {
            get
            {
                return this.cbEngine.Text;
            }
        }

        public string FileType
        {
            get
            {
                return this.cbFileType.Text;
            }
        }

        public AppSetting()
        {
            InitializeComponent();

            List<string> threshold = new List<string>();

            threshold.Add("Off");
            threshold.Add("Fatal");
            threshold.Add("Error");
            threshold.Add("Warn");
            threshold.Add("Info");
            threshold.Add("Debug");
            threshold.Add("All");

            cbThreshold.DataSource = threshold;

            cbThreshold.DisplayMember = "Name";

            XDocument appConfigX = XDocument.Load("Log4Net.config");
            XElement level = appConfigX.Root.Element("log4net").Element("root").Element("level");

            for (int i = 0; i < cbThreshold.Items.Count; i++)
            {
                if (cbThreshold.Items[i].ToString().ToLower() == level.Attribute("value").Value.ToLower())
                {
                    cbThreshold.SelectedIndex = i;
                }
            }
            
            cbFileType.SelectedIndex = 0;
            cbEngine.SelectedIndex = 0;
        }


        private void SetLogLevel(string threshold)
        {
            XDocument appConfigX = XDocument.Load("Log4Net.config");
            XElement level = appConfigX.Root.Element("log4net").Element("root").Element("level");
            level.SetAttributeValue("value", threshold);
            appConfigX.Save("Log4Net.config");            
        }


        private void buttonOK_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;

            SetLogLevel(cbThreshold.Text);

            this.Close();
        }


        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }
    }

}
